package com.jiamy.job;

import org.quartz.*;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class MyJob implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        // context-定时任务执行的环境（上下文）
        // 获取jobDetail
        JobDetail jobDetail = context.getJobDetail();
//        System.out.println("System.identityHashCode(jobDetail) = " + System.identityHashCode(jobDetail));

        // 输出详情
        /*System.out.println("任务名字：" + jobDetail.getKey().getName());
        System.out.println("任务分组名字：" + jobDetail.getKey().getGroup());
        System.out.println("任务类名字：" + jobDetail.getJobClass().getName());
        System.out.println("本次执行时间：" + context.getFireTime());
        System.out.println("下次执行时间：" + context.getNextFireTime());*/

        // 记录任务执行次数
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        Integer count = (Integer) jobDataMap.get("count");
        System.out.println("第" + count + "此执行");
        System.out.println("本次执行时间：" + context.getFireTime());
        jobDataMap.put("count", ++count);  //更新共享数据

        // 休眠8秒
        try {
            Thread.sleep(8000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
